<?php
class Application_Form_Segmento_Insere extends Zend_Form {
	
     private $codigo;
    
     public function __construct($codigo = null) {
        $this->codigo = $codigo;
        parent::__construct();
    }
    
    public function init(){
        $parceria = null;
        if(!$this->isNew()){
            $model = Application_Model_Segmento::getInstance();
            $parceria = $model->getById($this->codigo);
            
            $id = new Zend_Form_Element_Text('codigo');
            $id->setLabel('Código do Segmento');
            $id->setAttrib('readonly', 'readonly');
            $id->setValue($parceria['codigo']);
            $this->addElement($id);
        }
    
        $this->setAction ( '/segmento/insere/novo' );
        $this->setMethod ( 'post' );
        $this->setName ( 'pesquisa_conteudo_form' );

        $nome = new Zend_Form_Element_Text('descricao');
        $nome->setLabel('Nome do Segmento');
        $nome->setValue(!$this->isNew()?$parceria['descricao']:'');
        $this->addElement($nome);
        
        if($this->isNew()){
            $hdDtCadastro = new Zend_Form_Element_Hidden("dt_cadastro");
            $hdDtCadastro->setValue(Zend_Date::now()->get('Y/M/d'));
            $this->addElement($hdDtCadastro);
        }else{
            $hdDtCadastro = new Zend_Form_Element_Text("dt_cadastro");
            $hdDtCadastro->setLabel('Data de Cadastro');
            $hdDtCadastro->setAttrib('readonly', 'readonly');
            $hdDtCadastro->setValue($parceria['dt_cadastro']);
            $hdDtCadastro->addFilter(new Zend_Filter_Date());
            $this->addElement($hdDtCadastro);
        }
        
        $hdDtExclusao = new Zend_Form_Element_Hidden("dt_exclusao");
        $hdDtExclusao->addFilter(new Zend_Filter_Date());
        $this->addElement($hdDtExclusao);
        
        
        
        $this->getTableSegmentoAgenda();
    }
    

    private function isNew(){
        return !isset($this->codigo);
         
    }
    
    public function getTableSegmentoAgenda(){
        
        $segmentoAgendaModel = Application_Model_SegmentoAgenda::getInstance();
        $agSegmento = Array();
        
        if (isset($this->codigo)) {
            $agSegmento = $segmentoAgendaModel->getBySegmento($this->codigo);
        }
        
        //print_r($agSegmento);die();
        
        $table = '<dt id="agenda-label"><label class="optional">Horários do Segmento</label></dt><br/>
                  
                 <input type="button" class="btn btn-small" value="Adicionar linha" onclick="addRowAgenda();"/><br/><br/>
                    
                 <table class="table table-bordered table-striped" style="width:300px;" id="tblAgendaSegmento">
                    <thead>
                        <th>Hora</th>
                        <th>Ordem</th>
                    </thead>
                        <tbody>';
                        $nrInput=1;
                        foreach ($agSegmento as $hrSegmento => $dados){
                            $hora = $dados['hora'];
                            $ordem = $dados['ordem'];
                            $codigo = $dados['codigo'];
                            
                            $table .= '<tr>
                                            <td><input type="text" alt="time" class="valores input-small input-hora" value="' . $hora . '" name="hora_' . $nrInput . '"/></td>
                                            <td><input type="text" class="valores input-small input-ordem" value="' . $ordem . '" name="ordem_' . $nrInput . '"/> <input type="button" value="X" class="btnDelete" /><input name="codSegAgenda[]" type="hidden" value="' . $codigo . '"/></td>
                                       </tr>';
                            $nrInput++;
                        }
                        $table.= '</tbody>
                  </table>';
                        
                  $this->getView()->tableAgenda = $table;
    }
}